# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html

import sqlite3


class TencentspiderPipeline(object):

    def __init__(self):
        self.conn = sqlite3.connect("jobs.db")
        self.cursor = self.conn.cursor()
        self.cursor.execute('create table if not exists job_tb('
                            'id integer primary key autoincrement, '
                            'title, '
                            'publish_date, '
                            'content)')

    def process_item(self, item, spider):
        print("-------------")
        print("职位名称：", item['title'])
        print("发布时间：", item['publish_date'])
        print("工作内容：", str(item['content']).replace('\n', ''))
        self.cursor.execute('insert into job_tb values(null, ?, ?, ?)',
                            (item['title'], item['publish_date'], item['content']))
        self.conn.commit()
        return item

    def close_spider(self, spider):
        print('------------关闭连接')
        self.cursor.close()
        self.conn.close()
